import { login, getInfo, querySystemMenu, querySubsystemMenu, queryAllOrgTree, queryRoleValid } from '@/api/api'
import { getToken, setToken, removeToken } from '@/utils/auth'

// import Utils from '@/utils/index.js'

const state = {
  token: getToken(),
  username: '',
  avatar: '',
  departmentList: '',
  roleList: '',
  subsystemMenuMarkList: ''

}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, username) => {
    state.username = username
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_DEPARTMENT: (state, departmentList) => {
    state.departmentList = departmentList
  },
  SET_ROLE: (state, roleList) => {
    state.roleList = roleList
  },
  SET_MARK: (state, subsystemMenuMarkList) => {
    state.subsystemMenuMarkList = subsystemMenuMarkList
  }

}

const actions = {
  // 登录
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(res => {
        if (res.code === '0000') {
          commit('SET_TOKEN', res.data.token)
          commit('SET_NAME', username)
          setToken(res.data.token)
          resolve(res)
        } else {
          reject(res)
          // resolve()
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取用户信息
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo({ username: state.username }).then(res => {
        if (res.code === '0000') {
          const { data } = res
          commit('SET_NAME', state.username)
          // commit('SET_AVATAR', avatar)
          resolve(data)
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 移除token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  },

  // 获取部门信息
  getDepartmentList({ commit }) {
    return new Promise((resolve, reject) => {
      queryAllOrgTree({ status: '1' }).then(res => {
        if (res.code === '0000') {
          const { data } = res
          commit('SET_DEPARTMENT', res.data)
          resolve(data)
        } else {
          commit('SET_DEPARTMENT', [])
          console.log(res.msg)
        }
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 获取角色信息
  getRoleList({ commit }) {
    return new Promise((resolve, reject) => {
      queryRoleValid().then(res => {
        if (res.code === '0000') {
          const { data } = res
          commit('SET_ROLE', res.data)
          resolve(data)
        } else {
          commit('SET_ROLE', [])
          console.log(res.msg)
        }
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 获取权限标识列表
  getSubsystemMenuMarkList({ commit }, subsystemMenuMarkList) {
    commit('SET_MARK', subsystemMenuMarkList)
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

